﻿var request =  new XMLHttpRequest();
var price = new Array;
var open = new Array;
var high  = new Array;
var low   = new Array;
var html_source;

var Price_str="Price ";
var Open_str="Open ";
var High_str="High ";
var Low_str="Low ";
var Not_Listed_str="Not listed";
var stock_listed=false;

var len_search_str;
var search_str_pos;

var start_str = "<br><font style=\"font-family:Arial; font-size:9pt\">";
var start_pos;
var len_start_string;

var end_str = "</td>";
var end_pos;
var first_low_pos;

var final_str_start_pos;
var len_final_str;
var	final_str;

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
function view_onopen() {
	txtQuote.focus();
	reset_all();
}


function btnReset_onclick() {
	reset_all();
}


function btnFind_onclick() {
	alert("This functionality is yet to be implemented. \nPlease wait till the next version!!");
}


function btnGetPrice_onclick() {
	reset_all();
	get_stock_price();	
	get_stock_price();	
	fill_labels();
}

function reset_all() {
	stock_listed=false;
	lblWaiting.innerText="";
	lblBsePrice.innerText=0;
	lblBseOpen.innerText=0;
	lblBseHigh.innerText=0;
	lblBseLow.innerText=0;
	lblNsePrice.innerText=0;
	lblNseOpen.innerText=0;
	lblNseHigh.innerText=0;
	lblNseLow.innerText=0;
}


function get_stock_price() {
	var quote=txtQuote.value;
	var url="http://www.equitymaster.com/result.asp?symbol="+quote;
	request.open('GET', url, true);
	request.onreadystatechange=display_value;
	request.send(null);
}

function display_value() {
	var i,j;
	if (request.readyState == 4) 
	{
		lblWaiting.innerText="";
		html_source= request.responseText; 													// HTMLSource Code
		chk_Not_Listed();
		if (stock_listed) 
		{
			for (i=0; i<2; i++)
			{
				Offset = i * first_low_pos + 1;
				for (j=0; j<4; j++)
				{	
					switch(j)
					{
						case 0:				//Current Price
									search_str=Price_str;
									get_string();
									price[i]=final_str;
									break;
						case 1:				//Open Price							
									search_str=Open_str;
									get_string();
									open[i]=final_str;
									break;
						case 2:				//High Price
									search_str=High_str;
									get_string();
									high[i]=final_str;
									break;
						case 3:				//Low Price
									search_str=Low_str;
									get_string();
									low[i]=final_str;
									first_low_pos=search_str_pos;
									break;
						default:
									break;
					}
				}
			}
		}
		else
		{
					lblWaiting.innerText="Stock Not Listed!";
		}
	}
	else
	{
		lblWaiting.innerText="Please Wait";
	}
}

function get_string() 
{
	search_str_pos=html_source.indexOf(search_str, Offset);							// Find Position of Search String								
	len_search_str=search_str.length;																								// Find the Length of Search String
	len_search_str+=4;																																		// Add 4 for (Rs)
	start_pos=search_str_pos+len_search_str;																	// Find the Start Position of Font Tag
	len_start_str =  start_str.length;																									// Find the length of Font Tag
	final_str_start_pos = start_pos + len_start_str;															// Find the Start Position of our Required String
	end_pos=html_source.indexOf(end_str,final_str_start_pos);						// Find the Start Position of End String (</td> tag)
	len_final_str = end_pos - final_str_start_pos;																// Hence, find the length of our Required String
	
	final_str =  html_source.substr(final_str_start_pos, len_final_str );	
}


function chk_Not_Listed() {
		stock_listed=false;
		search_str_pos=html_source.search(Not_Listed_str);									// Find Position of "Not listed String								
		if (search_str_pos == -1) {
			stock_listed=true;
		}
}

function fill_labels() {
	for (var k=0;k<2;k++)
	{
		switch(k)
		{
			case 0:
						lblBsePrice.innerText=price[k];
						lblBseOpen.innerText=open[k];
						lblBseHigh.innerText=high[k];
						lblBseLow.innerText=low[k];
						break;
			case 1:
						lblNsePrice.innerText=price[k];
						lblNseOpen.innerText=open[k];
						lblNseHigh.innerText=high[k];
						lblNseLow.innerText=low[k];
						break;
			default:
						break;
			
		}
	}
}
